﻿using Common.CommonModel;
using Core.ApiFilter.Attributes;
using Core.AutoFacService.Attributes.InjectAttributes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.ApiFilter
{
    /// <summary>
    /// api结果包装过滤器
    /// </summary>
    [Component]
    public class ApiResultFilter : IResultFilter
    {

        public void OnResultExecuted(ResultExecutedContext context)
        {
            
        }

        public void OnResultExecuting(ResultExecutingContext context)
        {
            var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
            if (controllerActionDescriptor != null)
            {
                var isDefined = controllerActionDescriptor.EndpointMetadata.Any(a => a.GetType().Equals(typeof(ApiIgnoreAttribute)));
                if (isDefined)
                {
                    return;
                }
            }
            //对返回结果进行检查,如果没设置返回code和消息,那么默认置为成功
            if (context.Result != null)
            {
                if (context.Result is ObjectResult)
                {
                    var result = context.Result as ObjectResult;
                    if (result.StatusCode != 200)
                    {
                        return;
                    }
                    //如果已经做好返回模型,那么就判断code是否存在就可以了
                    if (result?.Value.GetType() == typeof(ResponseModel))
                    {
                        var res = result.Value as ResponseModel;
                        if (res?.code == ResponseTypeEnum.NotSetValue)
                        {
                            //返回数据对象为空 代表操作不是查询
                            if (res.model == null)
                            {
                                res.code = ResponseTypeEnum.OperationSucess;
                                res.message = ResponseTypeEnum.OperationSucess;
                            }
                            else
                            {
                                res.code = ResponseTypeEnum.GetInfoSucess;
                                res.message = ResponseTypeEnum.GetInfoSucess;
                            }
                        }
                        context.Result = new JsonResult(res);
                    }
                    else
                    {
                        context.Result = new JsonResult(new ResponseModel()
                        {
                            model = result?.Value,
                            code = ResponseTypeEnum.GetInfoSucess,
                            message = ResponseTypeEnum.GetInfoSucess
                        });
                    }



                }
                else if (context.Result is EmptyResult)
                {
                    return;
                }
                else if (context.Result is ContentResult)
                {
                    var result = context.Result as ContentResult;
                    context.Result = context.Result = new JsonResult(new ResponseModel()
                    {
                        code = ResponseTypeEnum.Success,
                        message = result?.Content
                    });
                }
                else
                {
                    throw new Exception($"未经处理的Result类型：{ context.Result.GetType().Name}");
                }

            }
        }
    }
}
